<?php

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Services\User\AddressService;
use Illuminate\Http\Request;

class AddressController extends WxController
{
    /**
     * 获取用户地址列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function list()
    {
        $list = AddressService::getInstance()->getAddressListByUserId($this->user()->id);
        return $this->successPaginate($list);
    }


    /**
     * 获取用户地址详情
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        $address = AddressService::getInstance()->getAddress($this->user()->id, intval($id));
        $data = [];
        if (!is_null($address)) {
            $data['name'] = $address->name;
            $data['province'] = $address->province;
            $data['city'] = $address->city;
            $data['county'] = $address->county;
            $data['address_detail'] = $address->address_detail;
            $data['area_code'] = $address->area_code;
            $data['postal_code'] = $address->postal_code;
            $data['tel'] = $address->tel;
            $data['is_default'] = $address->is_default;
        }
        return $this->success($data);
    }

    /**
     * 编辑用户地址详情
     * @param  Request  $request
     */
    public function save(Request $request)
    {
        $id = $request->input('id');
        $name = $request->input('name');
        $province = $request->input('province');
        $city = $request->input('city');
        $address_detail = $request->input('address_detail');
        $area_code = $request->input('area_code');
        $postal_code = $request->input('postal_code');
        $tel = $request->input('tel');
        $is_default = $request->input('is_default');
        $address = AddressService::getInstance()->getAddress($this->user()->id, intval($id));
        if (is_null($address)) {
            $this->failed(CodeResponse::PARAM_ILLEGAL);
        }
        if (!empty($name)) {
            $address->name = $name;
        }
        if (!empty($province)) {
            $address->province = $province;
        }
        if (!empty($city)) {
            $address->city = $city;
        }
        if (!empty($address_detail)) {
            $address->address_detail = $address_detail;
        }
        if (!empty($area_code)) {
            $address->area_code = $area_code;
        }
        if (!empty($postal_code)) {
            $address->postal_code = $postal_code;
        }
        if (!empty($tel)) {
            $address->tel = $tel;
        }
        if (!is_null($is_default)) {
            $address->is_default = intval($is_default);
        }
        $res = $address->save();
        return $this->failOrSuccess($res, CodeResponse::UPDATED_FAIL);
    }

    /**
     * 删除用户地址
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id) || !is_numeric($id)) {
            $this->failed(CodeResponse::PARAM_ILLEGAL);
        }
        AddressService::getInstance()->delete($this->user()->id, $id);
        return $this->success();

    }
}
